<!-- wangEditor富文本编辑器示例 -->
<script setup>
import WangEditor from '@/components/WangEditor/index.vue'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'

const content = ref('<p>这是富文本编辑器的测试内容</p>')
const readonly = ref(false)
const editorRef = ref()

const toggleReadonly = () => {
  readonly.value = !readonly.value
  ElMessage.success(readonly.value ? '已切换到只读模式' : '已启用编辑模式')
}

const handleChange = (data) => {
  console.log('内容变化:', data.html)
}

const handleCreated = (editor) => {
  console.log('编辑器创建完成:', editor)
}

const clearContent = () => {
  editorRef.value?.clearContent()
  ElMessage.success('内容已清空')
}

const setSampleContent = () => {
  const sampleHtml = `
    <h2>示例内容</h2>
    <p>这是一个<strong>富文本编辑器</strong>的示例内容。</p>
    <ul>
      <li>支持<em>粗体</em>和<em>斜体</em></li>
      <li>支持列表</li>
      <li>支持链接</li>
    </ul>
    <p>您可以在这里编辑内容。</p>
  `
  editorRef.value?.setContent(sampleHtml)
  ElMessage.success('示例内容已设置')
}

const getContent = () => {
  const contentData = editorRef.value?.getContent()
  console.log('当前内容:', contentData)
  ElMessage.success('内容已获取，请查看控制台')
}
</script>

<template>
  <div class="wang-editor-demo">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>WangEditor 富文本编辑器测试</span>
          <el-button @click="toggleReadonly" type="primary" size="small">
            {{ readonly ? '启用编辑' : '只读模式' }}
          </el-button>
        </div>
      </template>
      
      <div class="demo-content">
        <div class="demo-section">
          <h3>编辑模式</h3>
          <WangEditor 
            ref="editorRef"
            v-model="content" 
            :readonly="readonly"
            :height="'300px'"
            @change="handleChange"
            @created="handleCreated"
          />
        </div>
        
        <div class="demo-section">
          <h3>内容预览</h3>
          <div class="content-preview" v-html="content"></div>
        </div>
        
        <div class="demo-section">
          <h3>操作按钮</h3>
          <el-button @click="clearContent" type="warning" size="small">清空内容</el-button>
          <el-button @click="setSampleContent" type="primary" size="small">设置示例内容</el-button>
          <el-button @click="getContent" type="info" size="small">获取内容</el-button>
        </div>
      </div>
    </el-card>
  </div>
</template>

<style scoped>
.wang-editor-demo {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.demo-content {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.demo-section {
  border: 1px solid #e4e7ed;
  border-radius: 4px;
  padding: 15px;
}

.demo-section h3 {
  margin: 0 0 10px 0;
  color: #303133;
}

.content-preview {
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  padding: 15px;
  min-height: 100px;
  background-color: #fafafa;
}

.content-preview:empty::before {
  content: '暂无内容';
  color: #909399;
  font-style: italic;
}
</style>
